package com.example.tiptravel.view.activity;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;

import com.amap.api.location.AMapLocation;
import com.example.tiptravel.R;
import com.example.tiptravel.commom.base.BaseActivity;
import com.example.tiptravel.view.fragment.HomeFragment;
import com.example.tiptravel.view.fragment.PostFragment;
import com.example.tiptravel.view.fragment.ProfileFragment;
import com.ncapdevi.fragnav.FragNavController;
import com.roughike.bottombar.BottomBar;
import com.roughike.bottombar.OnTabSelectListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MainActivity extends BaseActivity implements OnTabSelectListener {


    @BindView(R.id.bb_main)
    BottomBar bbMain;
    private FragNavController controller;

    private List<Fragment> fragments = new ArrayList<>();

    private AMapLocation mapLocation;

    @Override
    protected int getLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        client.startLocation();
        bbMain.setAnimation(null);
    }

    @Override
    protected void initData() {
        fragments.add(HomeFragment.newInstance(mapLocation));
        fragments.add(PostFragment.newInstance());
        fragments.add(ProfileFragment.newInstance());
        controller = new FragNavController(getSupportFragmentManager(), R.id.fl_main, fragments);
        bbMain.setOnTabSelectListener(this);
    }

    @Override
    public void onTabSelected(@IdRes int tabId) {
        switch (tabId) {
            case R.id.tab_home:
                controller.switchTab(FragNavController.TAB1);
                break;
            case R.id.tab_post:
                controller.switchTab(FragNavController.TAB2);
                break;
            case R.id.tab_profile:
                controller.switchTab(FragNavController.TAB3);
                break;
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        client.stopLocation();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        client.onDestroy();
    }

    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        if (aMapLocation!=null&&aMapLocation.getErrorCode()==0){
            this.mapLocation = aMapLocation;
            initData();
        }
    }
}
